import { queryPage, queryInfoById, queryHistoryInfoById, queryHistoryPage, exportFile } from './service';
import { queryDictList } from '@/services/dict';

export default {
  namespace: 'device_alarm',

  state: {
    todoList: { data: [] },
    doneList: { data: [] },
    countList: { data: [] },
    historyList: { data: [] },
    details: {},
    historyDetails: {},
    todoCond: {},
    doneCond: {},
    historyCond: {},
    countCond: {},
    dict: {}
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {} });
      yield put({ type: 'setHistoryCond', payload: {} });
      yield put({ type: 'queryPage' })
    },
    *updateTodoCond({ payload }, { put }) {
      yield put({ type: 'setTodoCond', payload });
      yield put({ type: 'queryTodoPage' })
    },
    *updateDoneCond({ payload }, { put }) {
      yield put({ type: 'setDoneCond', payload });
      yield put({ type: 'queryDonePage' })
    },
    *updateCoundCond({ payload }, { put }) {
      yield put({ type: 'setCoundCond', payload });
      yield put({ type: 'queryCountPage' })
    },
    *queryTodoPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.device_alarm.todoCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      if (!params.state) params.state = '000';
      params.handle = 1;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setTodoPage', payload: { list, total } })
    },
    *queryDonePage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.device_alarm.doneCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      if (!params.state) params.state = '000';
      params.handle = 2;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setDonePage', payload: { list, total } })
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryHistoryInfoById({ payload }, { call, put }) {
      const response = yield call(queryHistoryInfoById, payload);
      yield put({ type: 'setHistoryInfo', payload: response })
    },
    *updateHistoryCond({ payload }, { put }) {
      yield put({ type: 'setHistoryCond', payload });
      yield put({ type: 'queryHistoryPage' })
    },
    *exportFile({ payload }, { put, call }) {
     const url = yield call(exportFile, payload)
     yield put({ type: 'setFileUrl', payload: url})
    },
    *queryHistoryPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.device_alarm.historyCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryHistoryPage, params);
      yield put({ type: 'setHistoryPage', payload: { list, total } })
    },
    *initHistory(_, {  put }) {
      yield put({ type: 'setHistoryCond', payload: {} });
      yield put({ type: 'setHistoryPage', payload: { list: [], total: 0 } })
    },
    *updateCountCond({ payload }, { put }) {
      yield put({ type: 'setCountCond', payload });
      yield put({ type: 'queryCountPage' })
    },
    *queryCountPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.device_alarm.countCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryHistoryPage, params);
      yield put({ type: 'setCountPage', payload: { list, total } })
    },
    *initCount(_, {  put }) {
      yield put({ type: 'setCountPage', payload: { list: [], total: 0 } })
    }
  },

  reducers: {
    setTodoCond(state, action) {
      return {
        ...state,
        todoCond: action.payload
      }
    },
    setTodoPage(state, action) {
      return {
        ...state,
        todoList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDoneCond(state, action) {
      return {
        ...state,
        doneCond: action.payload
      }
    },
    setDonePage(state, action) {
      return {
        ...state,
        doneList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setHistoryCond(state, action) {
      return {
        ...state,
        historyCond: action.payload
      }
    },
    setHistoryPage(state, action) {
      return {
        ...state,
        historyList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setCountCond(state, action) {
      return {
        ...state,
        countCond: action.payload
      }
    },
    setCountPage(state, action) {
      return {
        ...state,
        countList: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setHistoryInfo(state, action) {
      return {
        ...state,
        historyDetails: action.payload
      }
    },
    setFileUrl(state, action) {
      return {
        ...state,
        fileUrl: action.payload
      }
    },
  }
}
